package com.new2i.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection; 
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * csv文件解析用户类
 * 
 * @since 2015/09/05
 * @author 2i
 *
 */
public class CsvFileParseUtil {

	/**
	 * 将http上csv文件转化hashMap
	 * @param urlStr
	 * @return
	 */
	public static HashMap<String, String> toHashMapByUrlString(String urlStr) { 
		InputStream in = getInpuntStreamFromUrl(urlStr);
		List<List<String>> list = toListByInputSteam(in); 
		return toHashMapByList(list);
	}
	
	/**
	 * 将http上csv文件转化List
	 * @param urlStr
	 * @return
	 */
	public static List<List<String>> toListByUrlString(String urlStr) { 
		InputStream in = getInpuntStreamFromUrl(urlStr);
		return toListByInputSteam(in); 
	}
	
	/**
	 * 将http上文件转化为流
	 * 
	 * @since 2015/09/05
	 * @author 2i
	 * @param urlStr
	 * @return
	 */
	private static InputStream getInpuntStreamFromUrl(String urlStr) {
		try {
			URL url = new URL(urlStr);
		    HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
		    conn.setRequestMethod("GET"); 
		    if(conn.getResponseCode() == 200) { 
		    	return conn.getInputStream();
		    } 
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null; 
	}


	/**
	 * 解析csv文件的数据,返回hashMap
	 * 
	 * @since 2015/09/05
	 * @author 2i
	 * @return
	 */
	public static HashMap<String, String> toHashMapByFileName(String fileName) {
		try {
			InputStream in = new FileInputStream(fileName);
			List<List<String>> list = toListByInputSteam(in); 
			return toHashMapByList(list);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null; 
	}
	

	/**
	 * 通过文件名解析csv文件的数据,返回	HashMap
	 * 
	 * @since 2015/09/05
	 * @author 2i
	 * @return
	 */
	public static List<List<String>> toListByFileName(String fileName) {
		try {
			InputStream in = new FileInputStream(fileName);
			return toListByInputSteam(in);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 解析csv文件的数据,返回数组
	 * 
	 * @since 2015/09/05
	 * @author 2i
	 * @return
	 */
	private static List<List<String>> toListByInputSteam(InputStream in) {
		if(in == null)
			return null;
		List<List<String>> rlist = new ArrayList<>();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String line;
			while ((line = br.readLine()) != null) { 
				String[] info = line.split(",");
				List<String> itemList = Arrays.asList(info);
				rlist.add(itemList);
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rlist;
	}

	
	/**
	 * 将解析后的list,转化并返回hashMap
	 * 
	 * @since 2015/09/05
	 * @author 2i
	 * @return
	 */
	private static HashMap<String, String> toHashMapByList(List<List<String>> list) { 
		if (list == null || list.size() < 2) {
			return null;
		}
		HashMap<String, String> hashMap = new HashMap<String, String>();

		List<String> keyList = list.get(0);
		List<String> valueList = list.get(1);
		int count = Math.min(keyList.size(), valueList.size());
		int offset = 0;
		for (int i = 0; i < count; i++) {
			String key = keyList.get(i);
			String value = valueList.get(i + offset);
			boolean isCompleted = value.startsWith("\"") && value.endsWith("\"");
			if (!isCompleted) {
				offset++;
				value += valueList.get(i + offset);
			}
			key = key.replaceAll("\"", "");
			value = value.replaceAll("\"", "");
			hashMap.put(key, value);
		}
		return hashMap;
	}
	
}
